<!--数据字典管理-->
<template>
    <div>
        <div class="commonStyle nyContent">
            <div class="commonTit">数据字典管理</div>
            <a-tabs @change="changeTab" :animated="false" >
                <a-tab-pane v-for="item in nacosService" :key="item.serverCode" :tab="item.serverName" :name="item.serverCode">
                    <div class="formSearch">
                        <a-form layout="inline" @keydown.native.enter.prevent="handleSubmit">
                            <a-form-item label="关键字：" prop="searchKey">
                                <a-input v-model="searchKey" placeholder="请输入名称搜索"
                                         @keyup="formatEnterContent(searchKey)"
                                         @blur="formatEnterContent(searchKey)" style="width: 220px">
                                    <a-icon slot="prefix" type="user" class="displayPre" />
                                    <a-icon v-if="searchKey" slot="suffix" type="close-circle" @click="emitEmpty(searchKey)" />
                                </a-input>
                            </a-form-item>
                            <a-form-item label="类型名称：" prop="searchCode">
                                <a-select  v-model="sortCode" allowClear style="width:220px"
                                           placeholder="全部">
                                    <a-select-option v-for="item in sorts" :value="item.value" :key="item.value">{{
                                        item.label }}
                                    </a-select-option>
                                </a-select>
                            </a-form-item>
                            <a-button type="primary" @click="handleSubmit"
                                      v-if="hasPermission('sysdict:findSysDicts')">查询
                            </a-button>
                            <a-button style="margin-left: 8px" @click="reStore">重置</a-button>
                        </a-form>
                    </div>
                    <div style="float: right;" class="apply-title-btn-wrap">
                        <a-button type="primary" @click="addDict" v-if="hasPermission('sysdict:addSysDict')">新增</a-button>
                        <a-button type="danger" @click="delDict" v-if="hasPermission('sysdict:delSysDicts')" style="margin-left: 20px">批量删除</a-button>
                    </div>
                    <a-table :columns="columns" :dataSource="tableListData" :rowKey="record => record.id"
                             :pagination="false" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}">
                <span slot="index" slot-scope="text, record, index">
                    {{ (page.currentPage - 1) * page.pageSize + index + 1 }}
                </span>
                        <span slot="operation" slot-scope="text" class="table-operation">
                         <a @click="editSysDict(text)" >编辑</a>
                </span>
                    </a-table>
                    <Page :totalCount="pageVal.total" :currentPage="pageVal.currentPage" ref="page"
                          :pageSize="pageVal.pageSize"
                          @changePage="changePage" @changePageSize="changePageSize" />
                </a-tab-pane>
            </a-tabs>
        </div>
        <add-edit ref="addEdit" :serverCode="page.serverCode" :nacosService="nacosService" :sorts="sorts" :title="title" :sysDictDTO="sysDictDTO" @init="init" />
    </div>
</template>

<script>
    import addEdit from './addEdit.vue'
    import { mapState } from 'vuex'
    import Util from '@/libs/util'
    import Page from 'module/pagination'

    export default {
        name: 'sys_dict',
        components: {
            addEdit,
            Page
        },
        data () {
            return {
                title: true,
                showAddTable: false,
                showDelTable: false,
                searchKey: '',
                sortCode: undefined,
                flags: '',
                editButtonShow: false,
                array: [ 10, 20, 30, 40 ],
                /*选中的记录*/
                selectedRowKeys: [],
                sysDictDTO: {
                    id: '',
                    sortCode: '',
                    sortName: '',
                    code: '',
                    name: '',
                    createTime: '',
                    orderNum: ''
                },
                nacosService:[{serverCode: "micrApp", serverName: "全局参数"}].concat(JSON.parse(sessionStorage.getItem('nacosService'))),
                page: {
                    currentPage: 1,
                    pageSize: 10,
                    serverCode:'',
                },
                columns: [
                    {title: '序号', key: 'index', width: '7%', align: 'center', scopedSlots: {customRender: 'index'}},
                    {title: '类型', dataIndex: 'sortCode', key: 'sortCode'},
                    {title: '类型名称', dataIndex: 'sortName', key: 'sortName'},
                    {title: '代码', dataIndex: 'code', key: 'code'},
                    {title: '名称', dataIndex: 'name', key: 'name'},
                    {title: '创建时间', dataIndex: 'createTime', key: 'createTime'},
                    {title: '操作', key: 'operation', width: '13%', scopedSlots: {customRender: 'operation'}}
                ],
                tableListData:[],
                currentTabName: '',
            }
        },
        computed: {
            getsysDictDTOs () {
                return this.$store.state.sysDict.sysDictDTOs
            },
            ...mapState ({
                pageVal: state => state.sysDict.page
            }),
            sorts () {
                return this.$store.state.sysDict.sorts
            }
        },
        watch: {
            getsysDictDTOs ( val ) {
                this.tableListData = val
            }
        },
        methods: {
            /**
             * Function    : changeTab
             * Description : 切换tab页触发函数
             * Input       : name tab页名称
             * Return      : none
             */
            changeTab ( name ) {
                this.currentTabName = name
                this.page.serverCode = name
                this.page.currentPage = 1
                this.init ()
            },
            emitEmpty () {
                this.searchKey = ''
                this.init ()
            },
            reStore(){
                this.searchKey = ''
                this.sortCode = undefined
                this.page.currentPage = 1
                this.init();
            },
            /*
             * 过滤输入框输入的特殊字符
             * */
            formatEnterContent ( value ) {
                let a = Util.clearSpecialChart(value);
                this.searchKey = a
            },
            /**
             * Function    : init
             * Description : 初始化列表
             * Input       : none
             * Return      : none
             */
            init () {
                let para = Object.assign ({},
                  {searchKey: this.searchKey.trim ()},
                  {sortCode: this.sortCode}, this.page)
                this.selectedRowKeys = []
                this.tableListData = []
                this.$store.dispatch ('findSysDictsAct', para)
                this.$store.dispatch ('loadSortsAct',{serverCode:this.page.serverCode})
            },

            /**
             * Function    : addDict
             * Description : 新增数据字典
             * Input       : none
             * Return      : none
             */
            addDict () {
                this.sysDictDTO = {
                    id: '',
                    sortCode: '',
                    sortName: '',
                    code: '',
                    name: '',
                    createTime: '',
                    orderNum: 0
                }
                this.title = true
                this.$refs.addEdit.showModal ()
            },
            editSysDict ( text ) {
                this.showAddTable = true
                this.title = false
                this.$refs.addEdit.showEdit (text)
            },

            /**
             * Function    : onSelectChange
             * Description : 选择当前行id
             * Input       : none
             * Return      : none
             */
            onSelectChange (selectedRowKeys) {
                this.selectedRowKeys = selectedRowKeys
            },
            /**
             * Function    : delDict
             * Description : 批量删除数据字典信息
             * Input       : none
             * Return      : none
             */
            delDict () {
                var ids = ''
                var cArry = []
                this.selectedRowKeys.forEach (( item, index ) => {
                    if (index === 0) {
                        ids = item.id
                    } else {
                        ids += ',' + item.id
                    }
                    cArry.push (index)
                })
                if (this.selectedRowKeys.length <= 0) {
                    cArry =[];
                    this.$message.info ('请至少选择一条数据字典记录',3)
                    return
                }
               // ids = this.selectedRowKeys.join(',')
                this.$confirm ({
                    title: `确认删除选中的数据字典信息吗？`,
                    okText: '确定',
                    cancelText: '取消',
                    onOk: () => {
                        let _this = this
                        this.$store.dispatch ('batchDelSysDictsAct', this.selectedRowKeys).then (response => {
                            if (response.code === 0) {
                                if (( cArry.length === _this.tableListData.length ) && _this.page.currentPage > 1) {
                                    _this.page.currentPage = _this.page.currentPage - 1
                                }
                                _this.init ()
                                this.$message.success (response.message)
                            } else {
                                this.$message.error (response.message)
                            }
                            ids = ''
                            cArry = []
                        })
                    },
                    onCancel: () => {
                        ids = ''
                        cArry = []
                    }
                })
            },

            /**
             * Function    : changePage
             * Description : 切换页码
             * Input       : val
             * Return      : none
             */
            changePage ( val ) {
                this.page.currentPage = val
                this.init ()
            },

            /**
             * Function    : changePageSize
             * Description : 切换每页条数
             * Input       : vm
             * Return      : none
             */
            changePageSize ( vm ) {
                this.page.pageSize = vm
                this.page.currentPage = 1
                this.init ()
            },

            /**
             * Function    : handleSubmit
             * Description : 查询按钮
             * Input       : none
             * Return      : none
             */
            handleSubmit () {
                this.page.currentPage = 1
                this.init ()
            }
        },
        created () {
            // 显示
           /* this.editButtonShow = Util.hasPermission ('sysdict:updSysDict')
            if (!this.editButtonShow) {
                this.columns1 = this.columns1.splice (0, 7)
                this.columns1.shift ()
            }*/
            this.nacosService = this.nacosService.filter(item=>item.serverCode!=='sysconf'&& item.serverCode!=='audit'&& item.serverCode!=='sysstyle')
            console.log(this.nacosService)
            this.page.serverCode = 'micrApp'
            this.init ()
        }
    }
</script>
